package w83b.w83bOperacionesPaso.w83bPreavisos;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import w83b.w83bBeans.W83bPreavisosBean;
import w83b.w83bUtilidades.W83bUtilidadesComunes;
import w83b.w83bUtilidadesComunes.W83bClsConstantes;
import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;

// TODO: Auto-generated Javadoc
/**
 * The Class W83bTablaAgrupacionesOP.java.
 *
 * @author Deusto Sistemas
 */
public class W83bTablaPreavisosOPAction extends Q70ActionPasoOP{

	
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//Guardamos la accion en el contexto
			contexto.set("action",request.getParameter("action"));
			W83bPreavisosBean preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");

			if((StringUtils.isEmpty((String)contexto.get("modoPantalla"))
				||W83bClsConstantes.MODO_BUSQUEDA.equals(contexto.get("modoPantalla")))
				&&(!"sord".equals(request.getParameter("action")))){
				
			if("true".equals(request.getParameter(W83bClsConstantes.RIA_SEARCH))){
				preavisosBean= new W83bPreavisosBean();
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.anyoDesde"))){
					preavisosBean.setAnyoDesde(request.getParameter("w83bPreavisosBean.anyoDesde"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.anyoHasta"))){
					preavisosBean.setAnyoHasta(request.getParameter("w83bPreavisosBean.anyoHasta"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("codterrit_desde"))){
					preavisosBean.setCodTerritorioDesde(request.getParameter("codterrit_desde"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("codterrit_hasta"))){
					preavisosBean.setCodTerritorioHasta(request.getParameter("codterrit_hasta"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.desdeT"))){
					preavisosBean.setDesdeT(request.getParameter("w83bPreavisosBean.desdeT"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.hastaT"))){
					preavisosBean.setHastaT(request.getParameter("w83bPreavisosBean.hastaT"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_eleccion_fInicio"))){
					preavisosBean.setFecha_eleccion_desde(request.getParameter("fech_eleccion_fInicio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_eleccion_fFinal"))){
					preavisosBean.setFecha_eleccion_hasta(request.getParameter("fech_eleccion_fFinal"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_entrada_fInicio"))){
					preavisosBean.setFecha_entrada_desde(request.getParameter("fech_entrada_fInicio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_entrada_fFinal"))){
					preavisosBean.setFecha_entrada_hasta(request.getParameter("fech_entrada_fFinal"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_inicio_fInicio"))){
					preavisosBean.setFecha_inicio_desde(request.getParameter("fech_inicio_fInicio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_inicio_fInicio"))){
					preavisosBean.setFecha_inicio_desde(request.getParameter("fech_inicio_fInicio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_inicio_fFinal"))){
					preavisosBean.setFecha_inicio_hasta(request.getParameter("fech_inicio_fFinal"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.numpreaviso_desde"))){
					preavisosBean.setNumpreaviso_desde(request.getParameter("w83bPreavisosBean.numpreaviso_desde"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.numpreaviso_hasta"))){
					preavisosBean.setNumpreaviso_hasta(request.getParameter("w83bPreavisosBean.numpreaviso_hasta"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_tramite_fInicio"))){
					preavisosBean.setFecha_tramite_desde(request.getParameter("fech_tramite_fInicio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("fech_tramite_fFinal"))){
					preavisosBean.setFecha_tramite_hasta(request.getParameter("fech_tramite_fFinal"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.codTramite"))){
					preavisosBean.setCodTramite(request.getParameter("w83bPreavisosBean.codTramite"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.eleccion_parcial"))){
					preavisosBean.setEleccion_parcial(request.getParameter("w83bPreavisosBean.eleccion_parcial"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.preaviso_global"))){
					preavisosBean.setPreaviso_global(request.getParameter("w83bPreavisosBean.preaviso_global"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.utramite"))){
					preavisosBean.setUtramite(request.getParameter("w83bPreavisosBean.utramite"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.centroTrabajo.cifnif"))){
					preavisosBean.getCentroTrabajo().setCifnif(request.getParameter("w83bPreavisosBean.centroTrabajo.cifnif"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.centroTrabajo.numCentro"))){
					preavisosBean.getCentroTrabajo().setNumCentro(request.getParameter("w83bPreavisosBean.centroTrabajo.numCentro"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.centroTrabajo.razonSocial"))){
					preavisosBean.getCentroTrabajo().setRazonSocial(request.getParameter("w83bPreavisosBean.centroTrabajo.razonSocial"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.centroTrabajo.codTerritorio"))){
					preavisosBean.getCentroTrabajo().setCodTerritorio(request.getParameter("w83bPreavisosBean.centroTrabajo.codTerritorio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.centroTrabajo.codMunicipio"))){
					preavisosBean.getCentroTrabajo().setCodMunicipio(request.getParameter("w83bPreavisosBean.centroTrabajo.codMunicipio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.centroTrabajo.territorio"))){
					preavisosBean.getCentroTrabajo().setTerritorio(request.getParameter("w83bPreavisosBean.centroTrabajo.territorio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.centroTrabajo.municipio"))){
					preavisosBean.getCentroTrabajo().setMunicipio(request.getParameter("w83bPreavisosBean.centroTrabajo.municipio"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.idCentroTrabajo"))){
					preavisosBean.getCentroTrabajo().setCodCentroTrabajo(request.getParameter("w83bPreavisosBean.idCentroTrabajo"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.tramiteTxt"))){
					preavisosBean.setTramiteTxt(request.getParameter("w83bPreavisosBean.tramiteTxt"));
				}
				if (StringUtils.isNotEmpty(request.getParameter("w83bPreavisosBean.sinfechaeleccion"))){
					preavisosBean.setSinfechaeleccion(request.getParameter("w83bPreavisosBean.sinfechaeleccion"));
				}
					
	
							
				preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);
				
				contexto.set("w83bPreavisosBean",preavisosBean);
			}
			else
			{
				preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);
				
				contexto.set("w83bPreavisosBean",preavisosBean);

			}
		}else if("sord".equals(request.getParameter("action"))){
			preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
			preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);			
			contexto.set("w83bPreavisosBean",preavisosBean);
		}else if(StringUtils.isEmpty(((W83bPreavisosBean)contexto.get("w83bPreavisosBean")).getSort())){
			preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
			preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);			
			contexto.set("w83bPreavisosBean",preavisosBean);
		}else if(W83bClsConstantes.MODO_BUSQUEDA_CONBUSQ_INICIAL.equals(contexto.get("modoPantalla"))){
			preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
			preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);			
			contexto.set("w83bPreavisosBean",preavisosBean);
		}
		
	}

	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String action = (String)contexto.get("action");

		W83bPreavisosBean preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
		
		if(null==(ArrayList)contexto.get("w83bListaPreavisos")){	
			contexto.set("w83bListaPreavisos", new ArrayList());
		}
		if(preavisosBean.isCambioPagina()&&!("sord").equals(action)){
			return mapping.findForward("outOP");
		}
		if((StringUtils.isEmpty(request.getParameter("lanzarBusqueda")))){
			contexto.set("modoPantalla",W83bClsConstantes.MODO_BUSQUEDA);
			return mapping.findForward("outOP");
		}
		else
		{
			if (("find").equals(action)
					||"sord".equals(action)){
				//Si vengo de una pantalla anterior tengo que mirar si he realizado una búsqueda anterior
				if(StringUtils.isEmpty((String)contexto.get("modoPantalla"))
						||W83bClsConstantes.MODO_BUSQUEDA.equals(contexto.get("modoPantalla"))){
					return mapping.findForward("find");
				}else{
					contexto.set("modoPantalla",W83bClsConstantes.MODO_BUSQUEDA);
					contexto.set("migas","inicio.mantenimiento.agrupacion");
					if("true".equals((String)contexto.get("busqRealizada"))){
						return mapping.findForward("find");					
					}
				}
			}
		}
		

		
		return mapping.findForward("ok");
	}

}

